<?php

namespace app\api\controller;

use app\common\model\CaiPiaoModer;

class FenCaiController extends BaseApiController
{
    public array $notNeedLogin = ['getFuCai'];
    public function getFuCai()
    {
//        https://www.lottery.gov.cn/kj/kjlb.html?qxc
        $url = 'https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=1&isVerify=1&pageNo=';
        $pageNo = 1;
        while ($pageNo>0){
            $urlnew =  $url.$pageNo;
            $data = self::geturl($urlnew);
            return json($data);
//            $lotteryList = $data['value']['list'];
//            $len = count($lotteryList);
//            $all = [];
//            for($i = $len-1; $i >= 0; $i--){
//                $lotteryInfo = $lotteryList[$i];
//                $info['type'] = $lotteryInfo['lotteryGameNum'];
//                $info['draw_result'] = $lotteryInfo['lotteryDrawResult'];
//                $info['draw_pdf_url'] = $lotteryInfo['drawPdfUrl'];
//                $info['draw_time'] = $lotteryInfo['lotteryDrawTime'];
//                $info['draw_num'] = $lotteryInfo['lotteryDrawNum'];
//                $info['all_info'] = json_encode($lotteryInfo);
//                $all[] = $info;
//            }
//            $caipiao = new CaiPiaoModer();
//            $caipiao->saveAll($all);
            $pageNo--;
        }
        return json('添加完成');
    }

    public function geturl($url){
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
    }


    public function posturl($url,$data){
        $data  = json_encode($data);
        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output, true);
    }
}
